/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import dao.UserDAO;
import entities.User;
import game.Table;
import java.io.IOException;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.icefaces.application.PushRenderer;
import other.Costants;

/**
 *
 * @author alessandro
 */
@ManagedBean
@SessionScoped
public class UserManager implements Serializable {

    /** Creates a new instance of UserManager */
    private String username;
    private String password;
    private User user;
    private String state;

    /*tavolo*/
    private int bots;
    private boolean logged = false;
    private String errorMessage = null;
    @ManagedProperty(value = "#{tablesManager}")
    private TablesManager tablesManager;
    @ManagedProperty(value = "#{chatUserBean}")
    private ChatUserBean chatUserBean;

    public ChatUserBean getChatUserBean() {
        return chatUserBean;
    }

    public int getBots() {
        return bots;
    }

    public void setBots(int bots) {
        this.bots = bots;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setChatUserBean(ChatUserBean chatUserBean) {
        this.chatUserBean = chatUserBean;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public boolean isLogged() {
        return logged;
    }

    public void setLogged(boolean logged) {
        this.logged = logged;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public TablesManager getTablesManager() {
        return tablesManager;
    }

    public void setTablesManager(TablesManager tablesManager) {
        this.tablesManager = tablesManager;
    }

    public String login() {
        //@todo DAO?
        User userT = UserDAO.findUser(username, password);
        if (userT != null) {
            logged = true;
            this.user = userT;
            errorMessage = null;
            state = Costants.CHATTING;
            return Costants.HOME;
        } else {
            errorMessage = "User or password wrong. Please try again";
            return Costants.LOGIN;
        }

    }

    public boolean isLoggedIn() {
        return logged;
    }

    public String logout() {
        PushRenderer.removeCurrentView(Costants.PUSH_GROUP);
        PushRenderer.removeCurrentView(Costants.PUSH_TABLE);
        chatUserBean.removeFromList(username);
        tablesManager.leaveTable(username);
        logged = false;
        errorMessage = null;
        user = null;
        state = Costants.CHATTING;
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext ec = context.getExternalContext();
            ec.redirect("home.xhtml");
        } catch (Exception e) {
        }
        if(this.tableExist()) {
            tablesManager.leaveTable(username);
        }
        return Costants.HOME;
    }

    public boolean isChatting() {
        boolean flag = false;
        if (state.equals(Costants.CHATTING)) {
            flag = true;
        }

        return flag;
    }

    public boolean isWatching() {
        boolean flag = false;
        if (state.equals(Costants.WATCHING)) {
            flag = true;
        }

        return flag;
    }

    public boolean isWaiting() {
        boolean flag = false;
        if (state.equals(Costants.WAITING)) {
            flag = true;
        }

        return flag;
    }

    public void joinTable(String table) {
        PushRenderer.removeCurrentView(Costants.PUSH_GROUP);
        tablesManager.addWatcher(username, table);
        state = Costants.WATCHING;
        PushRenderer.render(Costants.PUSH_GROUP);

    }

    public boolean tableExist() {
        boolean flag = false;
        if (tablesManager.getTableByUser(username) != null) {
            flag = true;
        }

        return flag;
    }
}
